package com.itheima.dao;

import com.itheima.domain.Account;
import com.itheima.utils.DataSourceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import javax.xml.crypto.Data;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@Repository
public class AccountDao {
    @Autowired
    private DataSource dataSource;
    public void add(int id, double money){

        try {

            Connection connection = DataSourceUtil.getConnection(dataSource);

            PreparedStatement preparedStatement =connection.prepareStatement("update account set money=money+? where id=?");

            preparedStatement.setDouble(1,money);
            preparedStatement.setInt(2,id);
            preparedStatement.execute();


            preparedStatement.close();



        } catch (SQLException e) {

            throw new RuntimeException(e);
        }


    }
    public void sub(int id,double money){

        try{
            Connection connection = DataSourceUtil.getConnection(dataSource);

            PreparedStatement preparedStatement = connection.prepareStatement("update account set money=money-? where id=?");

            preparedStatement.setDouble(1,money);
            preparedStatement.setInt(2,id);
            preparedStatement.execute();

            preparedStatement.close();



        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


    }


    public void save(Account account) {
        System.out.println("完成了插入功能");
    }

    public void delete(int id) {
        System.out.println("完成了删除功能");
    }

    public Account findById(int id) {
        System.out.println("完成了查找功能");
        return null;
    }
}
